const WebBot = require('WebBot');//引用WebBot模块

//注册主函数，指定浏览器相关参数
//WebBot.registerMain(webMain, "127.0.0.1", 16660, {browserName:"chrome"});

//接管已打开浏览器
WebBot.registerMain(webMain, "127.0.0.1", 16660, {browserName:"chrome",debugPort:9222});

/**用作代码提示，webMain函数会被多次调用，注意使用全局变量
* @param {WebBot} webBot
*/
async function webMain(webBot)
{
   
    //设置隐式等待
    await webBot.setImplicitTimeout(5000);

    console.log("启动机器人任务");

    //每秒检查一次，是否有对应任务
    interalFunc = setInterval(task_list, 60000);

 

    //任务时间表
function task_list()
{
    var date   = new Date(); 
    let hour   =date.getHours(); // 时
    let minute = date.getMinutes();
    let time   = hour+":"+minute

    console.log("开始判断任务时间表");

//没分钟都刷新一下，防止浏览器假死
//时间久了，程序执行不成功，在处理eprcw的时候
//await webBot.refresh();

   

    if( time == "15:33")
    {
        eprcw_renew_cv()
    }
    
    else if( time == "21:33")
    {
        eprcw_renew_cv()
    } 
    else if( time == "15:44")
    {
        eprcw_sign()
    }

    else if( time == "17:33")
    {
        eprcw_renew_cv()
    } 


    else
    {
        
        console.log(time , " : 当前时间无任务")
        
        //刷新浏览器防止假死
        renew()
        //go2baidu()

    }



}



 //刷新防止浏览器假死
async function renew()
{
   await webBot.refresh();
}




//自动刷新招聘信息
async function eprcw_renew_cv()
{

    console.log("子任务自动刷新招聘信息");

    //设置隐式等待
    await webBot.setImplicitTimeout(5000);

    await webBot.goto("https://www.eprcw.com/member/login/company");
    
    await webBot.sleep(3000);
 
   
        await webBot.sendKeys("/html/body/div/section/section[1]/main/section/div/div[1]/div[3]/div[2]/div[1]/input","kulaex2")
        await webBot.sendKeys("/html/body/div/section/section[1]/main/section/div/div[1]/div[3]/div[2]/div[2]/input","phonix0083")
        
        await webBot.sleep(3000);
        console.log("发送账密完毕，等待点击确认")
        await webBot.clickElement('/html/body/div[1]/section/section[1]/main/section/div/div[1]/div[3]/button')
  

    await webBot.sleep(3000);
    //点击进入职位发布
    console.log("点击进入职位发布")
    await webBot.clickElement('/html/body/div/section/section/aside/div/ul/li[2]')

    await webBot.sleep(3000);
    console.log("点击刷新按钮")
    //点击刷新按钮
    await webBot.clickElement('/html/body/div/section/section/main/section[1]/div/div[1]/div/div[4]/div[1]/div[3]/table/tbody/tr/td[2]/div/div[2]/a')

    //点击确认刷新
    await webBot.clickElement('/html/body/div[2]/div/div[3]/button[2]')
    console.log("点击确认刷新")
 
await webBot.goto("https://www.eprcw.com/member/login/company");

    //关闭当前页面
    close_page(webBot)

}



//打开百度，用于显示进程是否进行中
async function go2baidu()
{
    await webBot.goto('https://www.baidu.com');
    console.log("有响应，但是没任务执行，我就执行百度");
}



//自动签到
async function eprcw_sign()
{

    console.log("自动签到");


    await webBot.goto("https://www.eprcw.com/member/login/company");
    
    await webBot.sleep(3000);
    let now_url = await webBot.getCurrentUrl();

    console.log(now_url)

    //自动登录了，就不登录过一次了
    if(now_url != "https://www.eprcw.com/member/company")
    {
        await webBot.sendKeys("/html/body/div/section/section[1]/main/section/div/div[1]/div[3]/div[2]/div[1]/input","kulaex2")
        await webBot.sendKeys("/html/body/div/section/section[1]/main/section/div/div[1]/div[3]/div[2]/div[2]/input","phonix0083")
        
        console.log("发送账密完毕，等待点击确认")
        await webBot.clickElement('/html/body/div[1]/section/section[1]/main/section/div/div[1]/div[3]/button')
    }


    //点击签到按钮
    await webBot.clickElement('/html/body/div[1]/section/section/main/section[1]/div/div[2]/div[1]/div[2]/button')

     
    //关闭当前页面
   await webBot.closePage();
    //close_page(webBot)

}







//判断是否最后一个标签，用于关闭谷歌网页。
async function close_page(webBot)
{
    let page_number = await webBot.getAllPageId();
    //成功返回页面ID数组,失败返回null
    if (page_number > 1)
    {
    await webBot.closePage();
    console.log("关闭当前页面")
    }
    else
    {
        console.log("最后一个页面了，不关闭")
    }
}


    
}




